//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package 力扣.题库.滑动窗口.定长滑动窗口;

import java.util.Arrays;

public class demo_1652 {
    public demo_1652() {
    }

    public int[] decrypt(int[] code, int k) {
        int n = code.length;
        int[] res = new int[code.length];
        if (k == 0) {
            Arrays.fill(res, 0);
        } else if (k > 0) {
            int sum = 0;

            for(int i = 1; i <= k; ++i) {
                sum += code[i];
            }

            for(int i = 0; i < res.length; ++i) {
                res[i] = sum;
                sum -= code[(i + 1) % n];
                sum += code[(i + k + 1) % n];
            }
        } else {
            int left = n + k;
            int sum = 0;

            for(int i = left; i <= n - 1; ++i) {
                sum += code[i];
            }

            for(int i = 0; i < res.length; ++i) {
                res[i] = sum;
                sum += code[i];
                sum -= code[left];
                left = (left + 1) % n;
            }
        }

        return res;
    }

    public int[] decrypt2(int[] code, int k) {
        int n = code.length;
        int[] ans = new int[n];
        int r = k > 0 ? k + 1 : n;
        k = Math.abs(k);
        int s = 0;

        for(int i = r - k; i < r; ++i) {
            s += code[i];
        }

        for(int i = 0; i < n; ++i) {
            ans[i] = s;
            s += code[r % n] - code[(r - k) % n];
            ++r;
        }

        return ans;
    }
}
